package database.keys;

import database.inter.IDB;
import database.singleDB.SingleDB;
import jodis.conn.impl.IntReply;
import jodis.conn.inter.IReply;

public class Key {
    public static IReply execTTL(IDB idb, String[] cmdLines) {
        String key = cmdLines[1];
        int res = 0;
        if (idb instanceof SingleDB) {
            SingleDB db = (SingleDB) idb;
            Long ttl = db.getTTL(key);
            Object obj = db.getEntity(key);
            if (obj == null) {
                res = -2;
            } else if (ttl == null) {
                res = -1;
            } else {
                long interval = (ttl-System.currentTimeMillis())/1000;
                res = (int)interval;
            }
        }
        return IntReply.makeIntReply(res);
    }
}
